package executor;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author tongchen
 * @create 2023-01-24 21:42
 */
public class demo1001 {
    public static void main(String[] args) throws InterruptedException {
        //创建基于CAS的原子类
        AtomicInteger atomicInteger = new AtomicInteger();
        //创建两个线程实现自增操作
         Thread t1 =new Thread(()->{
             for (int i = 0; i <50000 ; i++) {
                 atomicInteger.getAndIncrement();
             }
                 });
         Thread t2 =new Thread(()->{
             for (int i = 0; i <50000 ; i++) {
                 atomicInteger.getAndIncrement();
             }
                 });
         //两个线程均开始执行
        t1.start();
        t2.start();
        //等待两个线程均执行结束
        t1.join();
        t2.join();
        System.out.println(atomicInteger);
    }
}
